<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/16 0016
 * Time: 13:47
 */

namespace app\api\controller\v1;

import('WeChat.WxPayData',EXTEND_PATH);
import('WeChat.WxPayApi',EXTEND_PATH);

use Think\Log;

use app\api\controller\v1\WechatAppPay;

class WxPay extends Base
{
    private $appkey = "7bcd0a23a6d669321806264b";
    private $masterSecret = "a4151e9aa126ba381abc4434";
    public function weipayverify()
    {
        $config = config("pay.WXCONF");
        $wxappid = $config['appid'];
        $mch_id = $config['mch_id'];
        $notify_url = 'http://www.qnn.com/app.php/WxPay/weipayverify';
        $wxkey = $config['key'];
        $wechatAppPay = new wechatAppPay($wxappid, $mch_id, $notify_url, $wxkey);
        $data = $wechatAppPay->getNotifyData();
        if ($wechatAppPay->verifyNotify($data)) {
            $demandModel = M('Demand');
            $orderdata = $demandModel->field('id,uid,order_code,title,order_type,total,yprice')->where(array('order_code' => $data['out_trade_no']))->find();
            $savedata['is_end'] = 1;
            $savedata['order_status'] = '1';
            $savedata['pay_time'] = time();
            $demandModel->where(array('order_code' => $data['out_trade_no']))->save($savedata);
            $orderid = $orderdata['id'];
            $model = new \Think\Model();
            $ddata = $model->query("SELECT __USER__.UID AS USER_ID,\n" .
                "   __USER__.TYPE,\n" .
                "   __USER__.PROVINCE_NAME,\n" .
                "   __USER__.CITY_NAME,\n" .
                "   __USER__.AREA_NAME,\n" .
                "   __USER__.QTYPE,\n" .
                "   __DEMAND__.UID,\n" .
                "   __DEMAND__.CID,\n" .
                "   __DEMAND__.province,\n" .
                "   __DEMAND__.city_name,\n" .
                "   __DEMAND__.pian_name,\n" .
                "   __DEMAND__.ISROB\n" .
                "   FROM __USER__,__DEMAND__\n" .
                "   WHERE __USER__.PROVINCE_NAME=__DEMAND__.PROVINCE\n" .
                "   AND __USER__.QTYPE=__DEMAND__.CID\n" .
                "   AND __USER__.CITY_NAME=__DEMAND__.CITY_NAME\n" .
                "   AND __USER__.AREA_NAME=__DEMAND__.PIAN_NAME\n" .
                "   AND __DEMAND__.ID='$orderid'"
            );
            $arrid = [];
            foreach ($ddata as $k => $v) {
                $arrid[] = $v['user_id'];
            }
            if ($orderdata['order_type'] == 0) {
                $res = $this->addUserLog($orderdata, 0, 0);
            } elseif ($orderdata['order_type'] == 1) {
                $res = $this->addUserLog($orderdata, 1, 0);
            } else {
                $res = $this->addUserLog($orderdata, 2, 0);
            }
            $str = $wechatAppPay->replyNotify();
            Log::write($str);
        } else {
            Log::write("微信验证失败！");
        }
    }
/*
    public function weipayverify1()
    {
        $config = config("pay.WXCONF");
        $wxappid = $config['appid'];
        $mch_id = $config['mch_id'];
        $notify_url = 'http://www.91lianwo.com/app.php/WxPay/weipayverify1';
        $wxkey = $config['key'];
        $wechatAppPay = new wechatAppPay($wxappid, $mch_id, $notify_url, $wxkey);
        $data = $wechatAppPay->getNotifyData();
        if ($wechatAppPay->verifyNotify($data)) {
            $model = M('User');
            $reModel = M('recharge');
            $res = $reModel->where(array('order_code' => $data['out_trade_no']))->find();
            $logdata['uid'] = $res['uid'];
            $logdata['total'] = $res['money'];
            $logdata['id'] = $res['order_code'];
            $logdata['yprice'] = $res['money'];
            $this->addUserLog($logdata, 3, 0);
            $reModel->where(array('order_code' => $data['out_trade_no']))->save(array('status' => 1));
            $model->where(array('order_code' => $data['out_trade_no']))->setInc("wallet", $res['money']);
            $str = $wechatAppPay->replyNotify();
            Log::write($str);
        } else {
            Log::write("微信验证失败！");
        }
    }
*/
    public function rechargenotifyurl()
    {
        $config = config("pay.WXCONF");
        $wxappid = $config['shop_appid'];
        $mch_id = $config['shop_mch_id'];
        $wxkey = $config['shop_key'];
        $notify_url = 'http://www.91lianwo.com/app.php/WxPay/rechargenotifyurl';
        $wechatAppPay = new wechatAppPay($wxappid, $mch_id, $notify_url, $wxkey);
        $data = $wechatAppPay->getNotifyData();
        if ($wechatAppPay->verifyNotify($data)) {
            $model = M('User');
            $reModel = M('recharge');
            $res = $reModel->where(array('order_code' => $data['out_trade_no']))->find();
            $logdata['uid'] = $res['uid'];
            $logdata['total'] = $res['money'];
            $logdata['id'] = $res['order_code'];
            $logdata['yprice'] = $res['money'];
            $this->addUserLog($logdata, 3, 1);
            $reModel->where(array('order_code' => $data['out_trade_no']))->save(array('status' => 1));
            $model->where(array('order_code' => $data['out_trade_no']))->setInc("wallet", $res['money']);
            $str = $wechatAppPay->replyNotify();
            Log::write($str);
        } else {
            Log::write("微信验证失败！");
        }
    }

    public function wechatpaynotifyurl()
    {
        $config = config("pay.WXCONF");
        $wxappid = $config['shop_appid'];
        $mch_id = $config['shop_mch_id'];
        $wxkey = $config['shop_key'];
        $notify_url = 'http://www.91lianwo.com/app.php/WxPay/wechatpaynotifyurl';
        $wechatAppPay = new wechatAppPay($wxappid, $mch_id, $notify_url, $wxkey);
        $data = $wechatAppPay->getNotifyData();
        if ($wechatAppPay->verifyNotify($data)) {
            $model = M('Demand');
            $orderpayModel = M('Orderpay');
            $res = $orderpayModel->where(array('order_code' => $data['out_trade_no']))->find();
            $logdata['uid'] = $res['uid'];
            $logdata['total'] = $res['money'];
            $logdata['id'] = $res['order_code'];
            $logdata['yprice'] = $res['money'];
            $this->addUserLog($logdata, 7, 1);
            $orderpayModel->where(array('order_code' => $data['out_trade_no']))->save(array('status' => 1));
            $model->where(array('id' => $res['orderid']))->save(array('order_status' => 1));
            $model->where(array('id' => $res['orderid']))->save(array("total" => $res['money']));
            $orderdata = $model->find($res['orderid']);
            $client = new Client($this->appkey, $this->masterSecret);
            $result = $client->push()
                ->setPlatform(array('ios', 'android'))
                ->addAlias((string)$orderdata['uid'])
                ->setNotificationAlert('您的'.$orderdata['title']."订单已支付,请去查看！")
                ->options(array(
                    "apns_production" => true
                ))
                ->send();
            $str = $wechatAppPay->replyNotify();
            Log::write($str);
        } else {
            Log::write("微信验证失败！");
        }
    }

}